package com.java2007.hotel.controller.backend;

import com.java2007.hotel.constant.ServletConstant;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Admin;
import com.java2007.hotel.service.AdminService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ghy
 * @version 1.0
 * @date 2021-01-25
 */
@WebServlet("/admin")
public class AdminController extends BaseServlet {

    private AdminService adminService = (AdminService) BeanFactory.getBean("com.java2007.hotel.service.impl.AdminServiceImpl");


    public String login(HttpServletRequest request, HttpServletResponse response) throws Exception{

        try {
            String adminName = request.getParameter("adminName");
            String password = request.getParameter("password");

            Admin admin = adminService.login(adminName, password);
            request.getSession().setAttribute("loginAdmin", admin);
            return ServletConstant.PRE_REDIRECT + "/index.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginFailMsg", e.getMessage());
            return ServletConstant.PRE_FORWARD + "/backend/login.jsp";
        }
    }

}
